<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

/**
 * 项目入口引导文件
 *
 * @package    script
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

/**
 * 设定页面编码
 */
@header("Content-type: text/html; charset=utf-8");

/**
 * 设定时区
 */
date_default_timezone_set('Asia/Shanghai');

/**
 * 设定项目开始时间
 */
define('START_TIME', microtime(true));

/**
 * 简化定义 PHP 路径常量
 */
!defined('DIR_SEP')  && define('DIR_SEP',  DIRECTORY_SEPARATOR);
!defined('PATH_SEP') && define('PATH_SEP', PATH_SEPARATOR);
!defined('__DIR__')  && define('__DIR__',  dirname(__FILE__));

/**
 * 定义项目目录结构
 */
define('EGP_APP_DIR', __DIR__);
define('EGP_WEB_DIR', dirname(EGP_APP_DIR) . DIR_SEP . 'webroot');
define('EGP_LIB_DIR', dirname(EGP_APP_DIR) . DIR_SEP . 'library');
define('EGP_CFG_DIR', EGP_APP_DIR . DIR_SEP . 'config');
define('EGP_MOD_DIR', EGP_APP_DIR . DIR_SEP . 'modules');
define('EGP_HLP_DIR', EGP_APP_DIR . DIR_SEP . 'helpers');
define('EGP_TMP_DIR', EGP_APP_DIR . DIR_SEP . 'temporary');

/**
 * 开启错误捕获
 */
@ini_set('track_errors', true);

/**
 * 报告所有错误
 */
error_reporting(E_ALL);

/**
 * 载入自定通用函数
 */
require(EGP_LIB_DIR . DIR_SEP . 'EGP' . DIR_SEP . 'Function.php');

/**
 * 载入文件加载类
 */
require(EGP_LIB_DIR . DIR_SEP . 'EGP' . DIR_SEP . 'Core.php');

/**
 * 设定 include_path
 */
EGP_Core::setIncludePath(EGP_LIB_DIR);

/**
 * 设定异常处理控制器
 */
set_exception_handler(array('EGP_Core', 'exceptionHandler'));

/**
 * 设定错误处理控制器
 */
set_error_handler(array('EGP_Core', 'errorHandler'));

/**
 * 注册自动加载类
 */
EGP_Core::registerAutoload();

/**
 * 加载全局配置文件
 */
$envConfig = EGP_Core::loadConfig('env');

/**
 * 设定调试模式
 */
EGP_Core::debug(isset($_GET['debug']) && $_GET['debug'] == 'true' ? true : $envConfig->debug);

/**
 * 设定 PHP 环境
 */
foreach ($envConfig->php as $key => $value)
    EGP_Ini::set($key, $value);
unset($envConfig, $key, $value);

/**
 * 运行环境配置文件
 */
$appConfig = EGP_Core::loadConfig('app');
EGP_Registry::set('config', $appConfig);

/**
 * 设定 Session 选项
 */
EGP_Session::setOptions($appConfig->session->toArray());

/**
 * 加载缓存适配器
 */
$cache = EGP_Cache::factory($appConfig->cache->adapter, $appConfig->cache->config);
isset($_GET['cache']) && $_GET['cache'] == 'clean' && $cache->clean();
EGP_Registry::set('cache', $cache);
unset($cache);

/**
 * 创建数据库连接
 */
$db = EGP_Db::factory($appConfig->db->adapter, $appConfig->db->config);
EGP_Registry::set('db', $db);
unset($db);

/**
 * 获取前端控制器
 */
$frontController = EGP_Controller_Front::getInstance();

/**
 * 扫描并加载 module
 */
$dirs = EGP_Io::scan(EGP_MOD_DIR);
foreach ($dirs as $dir) {
    $basedir = EGP_MOD_DIR . DIR_SEP . $dir . DIR_SEP;
    $options = array(
        'directory' => array(
            'controller' => $basedir . 'controllers',
            'model' => $basedir . 'models',
            'view' => $basedir . 'views',
        ),
    );
    $frontController->addModule($dir, $options);
}
unset($dirs, $dir, $basedir);

/**
 * 配置主机名
 */
if (isset($appConfig->hosts)) {
    foreach ($appConfig->hosts as $host => $module) {
        $module = $frontController->getModule($module);
        $module !== false && $module->addHost($host);
    }
    unset($module, $host);
}

/**
 * 根据主机名设定默认 module
 */
$httpHost = EGP_Http_Request::getInstance()->getHttpHost();
if (isset($appConfig->hosts->$httpHost))
    $frontController->setDefaultModule($appConfig->hosts->$httpHost);
unset($appConfig, $httpHost);

/**
 * run!!
 */
$frontController->dispatch();